﻿package com.calefk.modules.math {
	
    public class Vector3D {
        
        public var x:Number;
        public var y:Number;
        public var z:Number;
        
        public function Vector3D(px:Number = 0 , 
                py:Number = 0, pz:Number = 0) {
            x = px;
            y = py;
            z = pz;
        }
        
        public function add(v:Vector3D):void {
            x += v.x;
            y += v.y;
            z += v.z;
        }
        
        public function sub(v:Vector3D):void {
            x -= v.x;
            y -= v.y;
            z -= v.z;
        }
        
        public function length():Number {
            return Math.sqrt(x * x + y * y + z * z);
        }
        
        public function scale(s:Number = 1) {
            x *= s;
            y *= s;
            z *= s;
        }
        
        public function normalize(n:Number = 1) {
            var len:Number = length();
            if (len == 0) {
                len = 1;
            }
            scale(n / len);
        }
        
        public function setPosition(px:Number , py:Number , pz:Number):void {
            x = px;
            y = py;
            z = pz;
        }
        
        public function toZero():void {
            x = 0;
            y = 0;
            z = 0;
        }
        
        public function toString():String {
			return "{x:" + x + ",y:" + y + ",z:" + z + "}";
		}
        
        public function clone():Vector3D {
            return new Vector3D(x , y , z);
        }
        
        public static function dotProduct(va:Vector3D , vb:Vector3D):Number {
            return (va.x * vb.x) + (va.y * vb.y) + (va.z * vb.z);
        }
        
        public static function sum(va:Vector3D , vb:Vector3D):Vector3D {
            var v:Vector3D = va.clone();
            v.add(vb);
            return v;
        }
        
        public static function mult(va:Vector3D , s:Number):Vector3D {
            var v:Vector3D = va.clone();
            v.scale(s);
            return v;
        }
        
        public static function sub(va:Vector3D , vb:Vector3D):Vector3D {
            var v:Vector3D = va.clone();
            v.sub(vb);
            return v;
        }
        
        public static function rotateX(v:Vector3D , angleX:Number):void {
            var cos:Number = Math.cos(angleX);
            var sin:Number = Math.sin(angleX);
            
            var y1:Number = (cos * v.y) - (sin * v.z);
            var z1:Number = (cos * v.z) + (sin * v.y);
            
            v.y = y1;
            v.z = z1;
        }
        
         public static function rotateY(v:Vector3D , angleY:Number):void {
            var cos:Number = Math.cos(angleY);
            var sin:Number = Math.sin(angleY);
            
            var x1:Number = cos * v.x - sin * v.z;
            var z1:Number = cos * v.z + sin * v.x;
            
            v.x = x1;
            v.z = z1;
        }
        
        public static function rotateZ(v:Vector3D , angleZ:Number):void {
            var cos:Number = Math.cos(angleZ);
            var sin:Number = Math.sin(angleZ);
            
            var x1:Number = cos * v.x - sin * v.y;
            var y1:Number = cos * v.y + sin * v.x;
            
            v.x = x1;
            v.y = y1;
        }
        
        public static  function cross(v1:Vector3D, v2:Vector3D ):Vector3D {
			return new Vector3D(v1.y * v2.z - v1.z * v2.y, v1.z * v2.x - v1.x * v2.z, v1.x * v2.y - v1.y * v2.x);
		}

    }

}